/**
 * 用户相关rest api
 */
import request from '@/utils/request'

//API常量
const APIS = {
    V1: "/v1"
}

function queryByPaging(page, size, conditions) {
    return request({
        url: APIS.V1 + `/system/role/list/${page}/${size}`,
        method: 'GET',
        params: conditions
    })
}

function deleteById(id) {
    return request({
        url: APIS.V1 + `/system/role/${id}`,
        method: 'DELETE',
    })
}

function saveOrUpdate(role) {
    return request({
        url: APIS.V1 + `/system/role`,
        method: 'POST',
        data: role
    })
}

function assignRolePermission(roleId, permissionId) {
    return request({
        url: APIS.V1 + `/system/role/${roleId}/permission/${permissionId}`,
        method: 'POST'
    })
}

function removeAssignRolePermission(roleId, permissionId) {
    return request({
        url: APIS.V1 + `/system/role/${roleId}/${permissionId}`,
        method: 'DELETE'
    })
}



export function listTree(params) {
    return request({
        url: APIS.V1 + `/system/permission/listTree`,
        method: 'GET',
        params: params
    })
}

export default {
    queryByPaging,
    deleteById,
    saveOrUpdate,
    assignRolePermission,
    removeAssignRolePermission,
    listTree
}

// 角色列表
export function listRoles(param) {
    return request({
        url: APIS.V1 + `/system/role/listRoles`,
        method: 'get',
        params: param
    })
}

// 按照角色id查询该角色拥有的权限id
export function getPermissionByRoleId(roleId) {
    return request({
        url: APIS.V1 + `/system/permission/getPermissionByRoleId?roleId=` + roleId,
        method: 'get'
    })
}

// 按照用户id查询该角色拥有的权限id
export function getPermissionByUserId(userId) {
    return request({
        url: APIS.V1 + `/system/permission/getPermissionByUserId?userId=` + userId,
        method: 'get'
    })
}

export function saveAssignPermissionRole(data) {
    // alert(JSON.stringify(data));
    return request({
        url: APIS.V1 + `/system/role/saveAssignPermission/role`,
        method: 'POST',
        data
    })
}

export function saveAssignPermissionUser(data) {
    // alert(JSON.stringify(data));
    return request({
        url: APIS.V1 + `/system/role/saveAssignPermission/user`,
        method: 'POST',
        data
    })
}